package com.java.eight.stream;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

/**
 * @version v1.0
 * @description: 获得流的方式
 * @author: xiaoliang.liu
 * @date: 2018/7/8 21:50
 */
public class CreateStream {

    public static void main(String[] args) {
        // 从集合中获得流
//        Stream<String> stream = getStreamFromCollection();

        // 从stream中获得流
//        Stream<String> stream = getStreamFromValues();

        // 从数组中获得流
//        Stream<String> stream = getStreamFromArrays();

        // 从文件中获得流
//        Stream<String> stream = getStreamFromFile();
//        assert stream != null;

        // 从stream的迭代器中获得流(会有多个值，可用limit指定获得和个数)
//        Stream<Integer> stream = getStreamFromIterator();


        // 从stream的生成器中获得流(会有多个值，可用limit指定获得和个数)
//        Stream<Double> stream = getStreamFromGenerator();

        Stream<CustomStreamBean> stream = getStreamFromCustomGenerator();
        stream.forEach(System.out::println);

    }


    /**
     * 从集合中获得流
     *
     * @return stream
     */
    private static Stream<String> getStreamFromCollection() {
        List<String> list = Arrays.asList("hello", "world");
        return list.stream();
    }

    /**
     * 从stream中获得流
     *
     * @return stream
     */
    private static Stream<String> getStreamFromValues() {
        return Stream.of("hello", "stream");

    }

    /**
     * 从数组中获得流
     *
     * @return stream
     */
    private static Stream<String> getStreamFromArrays() {
        return Arrays.stream(new String[]{"hello", "array"});
    }

    /**
     * 从文件中获得流
     *
     * @return stream
     */
    private static Stream<String> getStreamFromFile() {
        Path path = Paths.get("E:/developer-kits/workspace/idea-workspace/java8/src/main/java/com/java/eight/stream/CreateStream.java");

        // try-catch方式1 会自动释放资源(前提是，这些可关闭的资源必须实现 java.lang.AutoCloseable 接口)
        // 这样调用返回的流是关闭的，不能这样用
//        try (Stream<String> lines = Files.lines(path)) {
//            return lines;
//        } catch (IOException e) {
//            e.printStackTrace();
//            throw new RuntimeException();
//        }


        // try-catch方式2
        try {
            return Files.lines(path);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException();
        }

    }

    /**
     * 从stream的迭代器中获得流(会有多个值，可用limit指定获得和个数)
     * @return stream
     */
    private static Stream<Integer> getStreamFromIterator() {
        return Stream.iterate(0, n -> n + 2).limit(10);
    }

    /**
     * 从stream的生成器中获得流(会有多个值，可用limit指定获得和个数)
     * @return stream
     */
    private static Stream<Double> getStreamFromGenerator() {
        return Stream.generate(Math::random).limit(15);
    }

    /**
     * 从stream的生成器中获得流(自定义supplier)
     * @return stream
     */
    private static Stream<CustomStreamBean> getStreamFromCustomGenerator() {
        return Stream.generate(new CustomStreamSupplier()).limit(5);
    }
}
